<?php

class Dashboard extends CI_Controller {

    public function __construct() {
        parent::__construct();
		//check session
		 $this->session->set_userdata(array('redirectUrl' =>current_url()));
		 check_user_login();
    }

    function index() {
		
        $hdata['pageTitle'] = 'Dashboard view';
    		$this->load->model('Classified_model','',TRUE);
    		$this->load->model('Automobile_model','',TRUE);
    		$this->load->model('Property_model','',TRUE);
    		
    		$cond['advertiserID'] = (int) $this->session->userdata('userid');
    		$hdata['classifieds_count'] = $this->Classified_model->count_result($cond);
    		$hdata['automobile_count'] = $this->Automobile_model->count_result($cond);
    		$hdata['property_count'] = $this->Property_model->count_result($cond);
        $hdata['news'] = $this->_get_news();

        $totalcount = (int)$hdata['classifieds_count'] + (int)$hdata['automobile_count'] + (int)$hdata['property_count'];
        $hdata['classifiendpercent'] = $this->_calculatepercentage($totalcount, (int)$hdata['classifieds_count']);
        $hdata['propertypercent'] = $this->_calculatepercentage($totalcount, (int)$hdata['property_count']);
        $hdata['autopercent'] = $this->_calculatepercentage($totalcount, (int)$hdata['automobile_count']);
        $this->load->view('admin/dashboard_view', $hdata);
    }
    function statistics(){
      
       $model = $_POST['model'];
       $advertiserID = (int)$_POST['advertiserID'];
       $status = array("STATUS"=>"false", "result"=> $advertiserID. $model);
       $cond['advertiserID'] = (int) $advertiserID;
       $this->load->model($model,'',TRUE);
       $status = $this->$model->get_statistics($cond);
       echo json_encode ($status);
     
    }
    function _get_news()
    {
        $filename = 'news'.'.txt';
        $textFile = base_url() . 'assets/data/' . $filename;
    

    $textRaw = file_get_contents($textFile);
        $news = $this->_read_file($textRaw);
        $results = array();
        foreach($news[1] as $new){
            $results[] = $new;
        }
        return $results;
    //$this->parser->parse('table_view', $data);
  }
  function _read_file($textRaw)
  {
    
    //$pattern = "'<p class=\"review\">([^<]*)</p>'si";
    $pattern = "'<description>(.*?)<\/description>'";
    preg_match_all($pattern, $textRaw, $matches);

    
    return $matches;


  }
  function _calculatepercentage($totalcount = 0, $value = 0)
  {
    if ($totalcount != 0)
      return ($value/$totalcount)*100;
    else
      return 0;
  }
    

}

?>
